package com.sls.web.manager.game.service.impl;

import java.util.LinkedList;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.SocketDTO;
import com.sls.core.socket.TransmissionCode;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.game.pojo.OrderInfo;
import com.sls.web.manager.game.querybean.OrderInfoQuery;
import com.sls.web.manager.game.service.OrderInfoService;
import com.sls.web.manager.page.Pagination;
import com.sls.web.manager.pojo.UserInfoVO;
import com.sls.web.manager.socket.SuperService;

/**
 * 订单详情Impl
 * 
 * @author cx
 * 
 */
@Repository("orderInfoService")
public class OrderInfoServiceImpl extends SuperService implements
		OrderInfoService {
	private Gson gson;

	public OrderInfoServiceImpl() {
		gson = CommonUtil.gson();
	}

	@Override
	public Pagination<OrderInfo> queryList(OrderInfoQuery orderInfoQuery,
			UserInfoVO userInfoVO) throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.ORDER_INFO_LIST);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, orderInfoQuery);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			Pagination<OrderInfo> order = gson.fromJson(body,
					new TypeToken<Pagination<OrderInfo>>() {
					}.getType());
			return order;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public OrderInfo queryById(String id, UserInfoVO userInfoVO)
			throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.ORDER_INFO_QUERY_BY_ID);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		OrderInfo order = new OrderInfo();
		order.setOrderId(id);
		SocketDTO dto = super.send(head, order);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			OrderInfo order2 = gson.fromJson(body, OrderInfo.class);
			return order2;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public boolean update(OrderInfo orderInfo, UserInfoVO userInfoVO)
			throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.ORDER_INFO_UPDATE);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		orderInfo.setOperator(userInfoVO.getUserName());
		SocketDTO dto = super.send(head, orderInfo);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			return true;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public String refund(OrderInfo orderInfo, UserInfoVO userInfoVO)
			throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.REFUND);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		orderInfo.setOperator(userInfoVO.getUserName());
		SocketDTO dto = super.send(head, orderInfo);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			if (StringUtils.isNotEmpty(body)) {
				String result = gson.fromJson(body, String.class);
				return result;
			}
			return null;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public List<String> querySupplyNameList(UserInfoVO userInfoVO)
			throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.QUERY_SUPPLY_NAME_LIST);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			if (StringUtils.isNotEmpty(body)) {
				List<String> nameList = gson.fromJson(body,
						new TypeToken<List<String>>() {
						}.getType());
				return nameList;
			}
			return null;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	public List<OrderInfo> queryOrderInfo(String supplyName, String isPay,
			String status, String downUserNo, String startTime, String endTime,
			UserInfoVO userInfoVO,String accountNo) throws Exception {
		OrderInfoQuery query = new OrderInfoQuery();
		query.setCurrentPage(1);
		query.setPageSize(30);
		query.setSupplyName(supplyName);
		query.setCreateOrderTimeStart(startTime);
		query.setCreateOrderTimeEnd(endTime);
		query.setIsPay(isPay);
		query.setStatus(status);
		query.setContractId(downUserNo);
		query.setRegisterAccount(accountNo);

		List<OrderInfo> orderList = new LinkedList<OrderInfo>();
		Pagination<OrderInfo> page = queryList(query, userInfoVO);
		if (page != null) {
			if (page.getList() != null) {
				orderList.addAll(page.getList());
			}
			int count = page.getRowCount();
			if (query.getPageSize() == 0) {
				query.setPageSize(1);
			}
			for (int i = 1; i < Math.ceil(count * 1.0 / query.getPageSize()); i++) {
				query.setCurrentPage(i + 1);
				page = queryList(query, userInfoVO);
				if (page.getList() != null) {
					orderList.addAll(page.getList());
				}
			}
			return orderList;
		}
		return null;
	}

	@Override
	public void manualNotify(String orderId, UserInfoVO userInfoVO)
			throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.MANUAL_NOTIFY_DOWN_USER);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		OrderInfo order = new OrderInfo();
		order.setOrderId(orderId);
		SocketDTO dto = super.send(head, order);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	/**
	 * 查询所有的工号
	 * @param userInfoVO
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<OrderInfo> queryAllAccountNo(OrderInfoQuery orderInfoQuery,UserInfoVO userInfoVO)
			throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("021013"); 
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		
		SocketDTO dto = super.send(head,orderInfoQuery);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			if (StringUtils.isNotEmpty(body)) {
				List<OrderInfo> nameList = gson.fromJson(body,
						new TypeToken<List<OrderInfo>>() {
						}.getType());
				return nameList;
			}
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
		return null;
	}

	@Override
	public Long queryOrderTotalPrice(OrderInfoQuery orderInfoQuery,
			UserInfoVO userInfoVO) throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("021036");
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, orderInfoQuery);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			if (StringUtils.isNotEmpty(body)) {
				Long result = gson.fromJson(body, Long.class);
				return result;
			}
			return null;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public Long queryOrderCostTotalPrice(OrderInfoQuery orderInfoQuery,
			UserInfoVO userInfoVO) throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("021037");
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, orderInfoQuery);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			if (StringUtils.isNotEmpty(body)) {
				Long result = gson.fromJson(body, Long.class);
				return result;
			}
			return null;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	
	}

	@Override
	public Long queryOrderTotalParvalue(OrderInfoQuery orderInfoQuery,
			UserInfoVO userInfoVO) throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("021038");
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, orderInfoQuery);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			if (StringUtils.isNotEmpty(body)) {
				Long result = gson.fromJson(body, Long.class);
				return result;
			}
			return null;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	
	}

	@Override
	public Long queryOrderTotalNum(OrderInfoQuery orderInfoQuery,
			UserInfoVO userInfoVO) throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("021039");
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, orderInfoQuery);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			if (StringUtils.isNotEmpty(body)) {
				Long result = gson.fromJson(body, Long.class);
				return result;
			}
			return null;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}
	
	
}
